home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / gplibs15.zoo / sbufvfor.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-01  |  19.8 KB  |  844 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18.  
  19. #if defined(LIBC_SCCS) && !defined(lint)
  20. static char sccsid[] = "%W% (Berkeley) %G%";
  21. #endif /* LIBC_SCCS and not lint */
  22.  
  23. /*
  24.  * Actual printf innards.
  25.  *
  26.  * This code is large and complicated...
  27.  */
  28.  
  29. #include <sys/types.h>
  30. #include <ioprivat.h>
  31. #include <string.h>
  32. #include <stdarg.h>
  33.  
  34. /*
  35.  * Define FLOATING_POINT to get floating point.
  36.  */
  37. #ifndef    NO_FLOATING_POINT
  38. #define    FLOATING_POINT
  39. #endif
  40.  
  41. /* end of configuration stuff */
  42.  
  43.  
  44. /*
  45.  * Helper class and function for `fprintf to unbuffered': creates a
  46.  * temporary buffer.  We only work on write-only files; this avoids
  47.  * worries about ungetc buffers and so forth.
  48.  */
  49.  
  50. class help_streambuf : public backupbuf {
  51.   public:
  52.     char *buffer;
  53.     size_t buf_size;
  54.     streambuf *sb;
  55.     help_streambuf(streambuf *sbuf, char *buf, size_t n) {
  56.     sb = sbuf; buffer = buf; buf_size = n;
  57.     setp(buffer, buffer+buf_size); }
  58.     ~help_streambuf();
  59.     virtual int overflow(int c = EOF);
  60. };
  61.  
  62. int help_streambuf::overflow(int c)
  63. {
  64.     long used = pptr() - pbase();
  65.     if (used) {
  66.     sb->sputn(pbase(), used);
  67.     pbump(-used);
  68.     }
  69.     if (c == EOF || buf_size == 0)
  70.     return sb->overflow(c);
  71.     return sputc(c);
  72. }
  73. help_streambuf::~help_streambuf()
  74. {
  75.     long used = pptr() - pbase();
  76.     if (used) {
  77.     sb->sputn(pbase(), used);
  78.     pbump(-used);
  79.     }
  80. }
  81.  
  82. int help_vform(streambuf *sb, char const *fmt0, va_list ap)
  83. {
  84.     char buf[_G_BUFSIZ]; // WARNING: on the atari _G_BUFSIZ is a
  85.              // variable. thats OK with GCC but other
  86.              // compilers will barf!
  87.  
  88.     help_streambuf helper(sb, buf, _G_BUFSIZ);
  89.     return helper.vform(fmt0, ap);
  90. }
  91.  
  92. #ifdef FLOATING_POINT
  93.  
  94. #include <floatio.h>
  95. #define    BUF        (MAXEXP+MAXFRACT+1)    /* + decimal point */
  96. #define    DEFPREC        6
  97. extern "C" double modf(double, double*);
  98.  
  99. #else /* no FLOATING_POINT */
  100.  
  101. #define    BUF        40
  102.  
  103. #endif /* FLOATING_POINT */
  104.  
  105.  
  106. /*
  107.  * Macros for converting digits to letters and vice versa
  108.  */
  109. #define    to_digit(c)    ((c) - '0')
  110. #define is_digit(c)    ((unsigned)to_digit(c) <= 9)
  111. #define    to_char(n)    ((n) + '0')
  112.  
  113. /*
  114.  * Flags used during conversion.
  115.  */
  116. #define    LONGINT        0x01        /* long integer */
  117. #define    LONGDBL        0x02        /* long double; unimplemented */
  118. #define    SHORTINT    0x04        /* short integer */
  119. #define    ALT        0x08        /* alternate form */
  120. #define    LADJUST        0x10        /* left adjustment */
  121. #define    ZEROPAD        0x20        /* zero (as opposed to blank) pad */
  122. #define    HEXPREFIX    0x40        /* add 0x or 0X prefix */
  123.  
  124. int streambuf::vform(char const *fmt0, _G_va_list ap)
  125. {
  126.     register const char *fmt; /* format string */
  127.     register int ch;    /* character from fmt */
  128.     register int n;        /* handy integer (short term usage) */
  129.     register char *cp;    /* handy char pointer (short term usage) */
  130.     const char *fmark;    /* for remembering a place in fmt */
  131.     register int flags;    /* flags as above */
  132.     int ret;        /* return value accumulator */
  133.     int width;        /* width from format (%8d), or 0 */
  134.     int prec;        /* precision from format (%.3d), or -1 */
  135.     char sign;        /* sign prefix (' ', '+', '-', or \0) */
  136. #ifdef FLOATING_POINT
  137.     int softsign;        /* temporary negative sign for floats */
  138.     double _double;        /* double precision arguments %[eEfgG] */
  139.     int fpprec;        /* `extra' floating precision in [eEfgG] */
  140. #endif
  141.     unsigned long _ulong;    /* integer arguments %[diouxX] */
  142.     enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
  143.     int dprec;        /* a copy of prec if [diouxX], 0 otherwise */
  144.     int fieldsz;        /* field size expanded by sign, etc */
  145.     int realsz;        /* field size expanded by dprec */
  146.     // The initialization of 'size' is to suppress a warning that
  147.     // 'size' might be used unitialized.  It seems gcc can't
  148.     // quite grok this spaghetti code ...
  149.     int size = 0;        /* size of converted field or string */
  150.     char buf[BUF];        /* space for %c, %[diouxX], %[eEfgG] */
  151.     char ox[2];        /* space for 0x hex-prefix */
  152.  
  153.     /*
  154.      * Choose PADSIZE to trade efficiency vs size.  If larger
  155.      * printf fields occur frequently, increase PADSIZE (and make
  156.      * the initialisers below longer).
  157.      */
  158. #define    PADSIZE    16        /* pad chunk size */
  159.     static char const blanks[PADSIZE] =
  160.      {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
  161.     static char const zeroes[PADSIZE] =
  162.      {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
  163.  
  164.     /*
  165.      * BEWARE, these `goto error' on error, and PAD uses `n'.
  166.      */
  167. #define    PRINT(ptr, len) \
  168.   do { if (sputn(ptr, len) != len) goto error; } while (0)
  169. #define    FLUSH() { }
  170. #if 1
  171. #define PAD_SP(howmany) if (padn(' ', howmany) < 0) goto error;
  172. #define PAD_0(howmany) if (padn('0', howmany) < 0) goto error;
  173. #else
  174. #define    PAD(howmany, with) { \
  175.     if ((n = (howmany)) > 0) { \
  176.         while (n > PADSIZE) { \
  177.             PRINT(with, PADSIZE); \
  178.             n -= PADSIZE; \
  179.         } \
  180.         PRINT(with, n); \
  181.     } \
  182. }
  183. #define PAD_SP(howmany) PAD(howmany, blanks)
  184. #define PAD_0(howmany) PAD(howmany, zeroes)
  185. #endif
  186.  
  187.     /*
  188.      * To extend shorts properly, we need both signed and unsigned
  189.      * argument extraction methods.
  190.      */
  191. #define    SARG() \
  192.     (flags&LONGINT ? va_arg(ap, long) : \
  193.         flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
  194.         (long)va_arg(ap, int))
  195. #define    UARG() \
  196.     (flags&LONGINT ? va_arg(ap, unsigned long) : \
  197.         flags&SHORTINT ? (unsigned long)(unsigned short)va_arg(ap, int) : \
  198.         (unsigned long)va_arg(ap, unsigned int))
  199.  
  200.     /* optimise cerr (and other unbuffered Unix files) */
  201.     if (unbuffered())
  202.         return help_vform(this, fmt0, ap);
  203.  
  204.     fmt = fmt0;
  205.     ret = 0;
  206.  
  207.     /*
  208.      * Scan the format for conversions (`%' character).
  209.      */
  210.     for (;;) {
  211.         for (fmark = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
  212.             /* void */;
  213.         if ((n = fmt - fmark) != 0) {
  214.             PRINT(fmark, n);
  215.             ret += n;
  216.         }
  217.         if (ch == '\0')
  218.             goto done;
  219.         fmt++;        /* skip over '%' */
  220.  
  221.         flags = 0;
  222.         dprec = 0;
  223. #ifdef FLOATING_POINT
  224.         fpprec = 0;
  225. #endif
  226.         width = 0;
  227.         prec = -1;
  228.         sign = '\0';
  229.  
  230. rflag:        ch = *fmt++;
  231. reswitch:    switch (ch) {
  232.         case ' ':
  233.             /*
  234.              * ``If the space and + flags both appear, the space
  235.              * flag will be ignored.''
  236.              *    -- ANSI X3J11
  237.              */
  238.             if (!sign)
  239.                 sign = ' ';
  240.             goto rflag;
  241.         case '#':
  242.             flags |= ALT;
  243.             goto rflag;
  244.         case '*':
  245.             /*
  246.              * ``A negative field width argument is taken as a
  247.              * - flag followed by a positive field width.''
  248.              *    -- ANSI X3J11
  249.              * They don't exclude field widths read from args.
  250.              */
  251.             if ((width = va_arg(ap, int)) >= 0)
  252.                 goto rflag;
  253.             width = -width;
  254.             /* FALLTHROUGH */
  255.         case '-':
  256.             flags |= LADJUST;
  257.             goto rflag;
  258.         case '+':
  259.             sign = '+';
  260.             goto rflag;
  261.         case '.':
  262.             if ((ch = *fmt++) == '*') {
  263.                 n = va_arg(ap, int);
  264.                 prec = n < 0 ? -1 : n;
  265.                 goto rflag;
  266.             }
  267.             n = 0;
  268.             while (is_digit(ch)) {
  269.                 n = 10 * n + to_digit(ch);
  270.                 ch = *fmt++;
  271.             }
  272.             prec = n < 0 ? -1 : n;
  273.             goto reswitch;
  274.         case '0':
  275.             /*
  276.              * ``Note that 0 is taken as a flag, not as the
  277.              * beginning of a field width.''
  278.              *    -- ANSI X3J11
  279.              */
  280.             flags |= ZEROPAD;
  281.             goto rflag;
  282.         case '1': case '2': case '3': case '4':
  283.         case '5': case '6': case '7': case '8': case '9':
  284.             n = 0;
  285.             do {
  286.                 n = 10 * n + to_digit(ch);
  287.                 ch = *fmt++;
  288.             } while (is_digit(ch));
  289.             width = n;
  290.             goto reswitch;
  291. #ifdef FLOATING_POINT
  292.         case 'L':
  293.             flags |= LONGDBL;
  294.             goto rflag;
  295. #endif
  296.         case 'h':
  297.             flags |= SHORTINT;
  298.             goto rflag;
  299.         case 'l':
  300.             flags |= LONGINT;
  301.             goto rflag;
  302.         case 'c':
  303.             *(cp = buf) = va_arg(ap, int);
  304.             size = 1;
  305.             sign = '\0';
  306.             break;
  307.         case 'D':
  308.             flags |= LONGINT;
  309.             /*FALLTHROUGH*/
  310.         case 'd':
  311.         case 'i':
  312.             _ulong = SARG();
  313.             if ((long)_ulong < 0) {
  314.                 _ulong = -_ulong;
  315.                 sign = '-';
  316.             }
  317.             base = DEC;
  318.             goto number;
  319. #ifdef FLOATING_POINT
  320.         case 'e':
  321.         case 'E':
  322.         case 'f':
  323.         case 'F':
  324.         case 'g':
  325.         case 'G':
  326.             _double = va_arg(ap, double);
  327. #ifdef USE_DTOA
  328.             {
  329.                 ios::fmtflags fmt_flags = 0;
  330.                 if (flags & ALT)
  331.                 fmt_flags |= ios::showpoint;
  332.                 if (flags & LADJUST)
  333.                 fmt_flags |= ios::right;
  334.                 if (__outfloat(_double, this, ch, width,
  335.                        prec < 0 ? DEFPREC : prec,
  336.                        fmt_flags,
  337.                        sign,
  338.                        flags & ZEROPAD ? '0' : ' ') < 0)
  339.                 goto error;
  340.             }
  341.             // CHECK ERROR!
  342.             continue;
  343. #else
  344.             /*
  345.              * don't do unrealistic precision; just pad it with
  346.              * zeroes later, so buffer size stays rational.
  347.              */
  348.             if (prec > MAXFRACT) {
  349.                 if ((ch != 'g' && ch != 'G') || (flags&ALT))
  350.                     fpprec = prec - MAXFRACT;
  351.                 prec = MAXFRACT;
  352.             } else if (prec == -1)
  353.                 prec = DEFPREC;
  354.             // __cvt_double may have to round up before the
  355.             // "start" of its buffer, i.e.
  356.             // ``intf("%.2f", (double)9.999);'';
  357.             // if the first character is still NUL, it did.
  358.             // softsign avoids negative 0 if _double < 0 but
  359.             // no significant digits will be shown.
  360.             cp = buf;
  361.             *cp = '\0';
  362.             size = __cvt_double(_double, prec, flags, &softsign,
  363.                         ch, cp, buf + sizeof(buf));
  364.             if (softsign)
  365.                 sign = '-';
  366.             if (*cp == '\0')
  367.                 cp++;
  368.             break;
  369. #endif
  370. #endif /* FLOATING_POINT */
  371.         case 'n':
  372.             if (flags & LONGINT)
  373.                 *va_arg(ap, long *) = ret;
  374.             else if (flags & SHORTINT)
  375.                 *va_arg(ap, short *) = ret;
  376.             else
  377.                 *va_arg(ap, int *) = ret;
  378.             continue;    /* no output */
  379.         case 'O':
  380.             flags |= LONGINT;
  381.             /*FALLTHROUGH*/
  382.         case 'o':
  383.             _ulong = UARG();
  384.             base = OCT;
  385.             goto nosign;
  386.         case 'p':
  387.             /*
  388.              * ``The argument shall be a pointer to void.  The
  389.              * value of the pointer is converted to a sequence
  390.              * of printable characters, in an implementation-
  391.              * defined manner.''
  392.              *    -- ANSI X3J11
  393.              */
  394.             /* NOSTRICT */
  395.             _ulong = (unsigned long)va_arg(ap, void *);
  396.             base = HEX;
  397.             flags |= HEXPREFIX;
  398.             ch = 'x';
  399.             goto nosign;
  400.         case 's':
  401.             if ((cp = va_arg(ap, char *)) == NULL)
  402.                 cp = "(null)";
  403.             if (prec >= 0) {
  404.                 /*
  405.                  * can't use strlen; can only look for the
  406.                  * NUL in the first `prec' characters, and
  407.                  * strlen() will go further.
  408.                  */
  409.                 char *p = (char*)memchr(cp, 0, prec);
  410.  
  411.                 if (p != NULL) {
  412.                     size = p - cp;
  413.                     if (size > prec)
  414.                         size = prec;
  415.                 } else
  416.                     size = prec;
  417.             } else
  418.                 size = strlen(cp);
  419.             sign = '\0';
  420.             break;
  421.         case 'U':
  422.             flags |= LONGINT;
  423.             /*FALLTHROUGH*/
  424.         case 'u':
  425.             _ulong = UARG();
  426.             base = DEC;
  427.             goto nosign;
  428.         case 'X':
  429.         case 'x':
  430.             _ulong = UARG();
  431.             base = HEX;
  432.             /* leading 0x/X only if non-zero */
  433.             if (flags & ALT && _ulong != 0)
  434.                 flags |= HEXPREFIX;
  435.  
  436.             /* unsigned conversions */
  437. nosign:            sign = '\0';
  438.             /*
  439.              * ``... diouXx conversions ... if a precision is
  440.              * specified, the 0 flag will be ignored.''
  441.              *    -- ANSI X3J11
  442.              */
  443. number:            if ((dprec = prec) >= 0)
  444.                 flags &= ~ZEROPAD;
  445.  
  446.             /*
  447.              * ``The result of converting a zero value with an
  448.              * explicit precision of zero is no characters.''
  449.              *    -- ANSI X3J11
  450.              */
  451.             cp = buf + BUF;
  452.             if (_ulong != 0 || prec != 0) {
  453.                     char *xdigs; /* digits for [xX] conversion */
  454.                 /*
  455.                  * unsigned mod is hard, and unsigned mod
  456.                  * by a constant is easier than that by
  457.                  * a variable; hence this switch.
  458.                  */
  459.                 switch (base) {
  460.                 case OCT:
  461.                     do {
  462.                         *--cp = to_char(_ulong & 7);
  463.                         _ulong >>= 3;
  464.                     } while (_ulong);
  465.                     /* handle octal leading 0 */
  466.                     if (flags & ALT && *cp != '0')
  467.                         *--cp = '0';
  468.                     break;
  469.  
  470.                 case DEC:
  471.                     /* many numbers are 1 digit */
  472.                     while (_ulong >= 10) {
  473.                         *--cp = to_char(_ulong % 10);
  474.                         _ulong /= 10;
  475.                     }
  476.                     *--cp = to_char(_ulong);
  477.                     break;
  478.  
  479.                 case HEX:
  480.                     if (ch == 'X')
  481.                         xdigs = "0123456789ABCDEF";
  482.                     else /* ch == 'x' || ch == 'p' */
  483.                         xdigs = "0123456789abcdef";
  484.                     do {
  485.                         *--cp = xdigs[_ulong & 15];
  486.                         _ulong >>= 4;
  487.                     } while (_ulong);
  488.                     break;
  489.  
  490.                 default:
  491.                     cp = "bug in vform: bad base";
  492.                     goto skipsize;
  493.                 }
  494.             }
  495.             size = buf + BUF - cp;
  496.         skipsize:
  497.             break;
  498.         default:    /* "%?" prints ?, unless ? is NUL */
  499.             if (ch == '\0')
  500.                 goto done;
  501.             /* pretend it was %c with argument ch */
  502.             cp = buf;
  503.             *cp = ch;
  504.             size = 1;
  505.             sign = '\0';
  506.             break;
  507.         }
  508.  
  509.         /*
  510.          * All reasonable formats wind up here.  At this point,
  511.          * `cp' points to a string which (if not flags&LADJUST)
  512.          * should be padded out to `width' places.  If
  513.          * flags&ZEROPAD, it should first be prefixed by any
  514.          * sign or other prefix; otherwise, it should be blank
  515.          * padded before the prefix is emitted.  After any
  516.          * left-hand padding and prefixing, emit zeroes
  517.          * required by a decimal [diouxX] precision, then print
  518.          * the string proper, then emit zeroes required by any
  519.          * leftover floating precision; finally, if LADJUST,
  520.          * pad with blanks.
  521.          */
  522.  
  523.         /*
  524.          * compute actual size, so we know how much to pad.
  525.          * fieldsz excludes decimal prec; realsz includes it
  526.          */
  527. #ifdef FLOATING_POINT
  528.         fieldsz = size + fpprec;
  529. #else
  530.         fieldsz = size;
  531. #endif
  532.         if (sign)
  533.             fieldsz++;
  534.         else if (flags & HEXPREFIX)
  535.             fieldsz += 2;
  536.         realsz = dprec > fieldsz ? dprec : fieldsz;
  537.  
  538.         /* right-adjusting blank padding */
  539.         if ((flags & (LADJUST|ZEROPAD)) == 0)
  540.             PAD_SP(width - realsz);
  541.  
  542.         /* prefix */
  543.         if (sign) {
  544.             PRINT(&sign, 1);
  545.         } else if (flags & HEXPREFIX) {
  546.             ox[0] = '0';
  547.             ox[1] = ch;
  548.             PRINT(ox, 2);
  549.         }
  550.  
  551.         /* right-adjusting zero padding */
  552.         if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
  553.             PAD_0(width - realsz);
  554.  
  555.         /* leading zeroes from decimal precision */
  556.         PAD_0(dprec - fieldsz);
  557.  
  558.         /* the string or number proper */
  559.         PRINT(cp, size);
  560.  
  561. #ifdef FLOATING_POINT
  562.         /* trailing f.p. zeroes */
  563.         PAD_0(fpprec);
  564. #endif
  565.  
  566.         /* left-adjusting padding (always blank) */
  567.         if (flags & LADJUST)
  568.             PAD_SP(width - realsz);
  569.  
  570.         /* finally, adjust ret */
  571.         ret += width > realsz ? width : realsz;
  572.  
  573.         FLUSH();    /* copy out the I/O vectors */
  574.     }
  575. done:
  576.     FLUSH();
  577.     return ret;
  578. error:
  579.     return EOF;
  580.     /* NOTREACHED */
  581. }
  582.  
  583. #if defined(FLOATING_POINT) && !defined(USE_DTOA)
  584.  
  585. static char *exponent(register char *p, register int exp, int fmtch)
  586. {
  587.     register char *t;
  588.     char expbuf[MAXEXP];
  589.  
  590.     *p++ = fmtch;
  591.     if (exp < 0) {
  592.         exp = -exp;
  593.         *p++ = '-';
  594.     }
  595.     else
  596.         *p++ = '+';
  597.     t = expbuf + MAXEXP;
  598.     if (exp > 9) {
  599.         do {
  600.             *--t = to_char(exp % 10);
  601.         } while ((exp /= 10) > 9);
  602.         *--t = to_char(exp);
  603.         for (; t < expbuf + MAXEXP; *p++ = *t++);
  604.     }
  605.     else {
  606.         *p++ = '0';
  607.         *p++ = to_char(exp);
  608.     }
  609.     return (p);
  610. }
  611.  
  612. static char * round(double fract, int *exp,
  613.             register char *start, register char *end,
  614.             char ch, int *signp)
  615. {
  616.     double tmp;
  617.  
  618.     if (fract)
  619.     (void)modf(fract * 10, &tmp);
  620.     else
  621.         tmp = to_digit(ch);
  622.     if (tmp > 4)
  623.         for (;; --end) {
  624.             if (*end == '.')
  625.                 --end;
  626.             if (++*end <= '9')
  627.                 break;
  628.             *end = '0';
  629.             if (end == start) {
  630.                 if (exp) {    /* e/E; increment exponent */
  631.                     *end = '1';
  632.                     ++*exp;
  633.                 }
  634.                 else {        /* f; add extra digit */
  635.                 *--end = '1';
  636.                 --start;
  637.                 }
  638.                 break;
  639.             }
  640.         }
  641.     /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */
  642.     else if (*signp == '-')
  643.         for (;; --end) {
  644.             if (*end == '.')
  645.                 --end;
  646.             if (*end != '0')
  647.                 break;
  648.             if (end == start)
  649.                 *signp = 0;
  650.         }
  651.     return (start);
  652. }
  653.  
  654. int __cvt_double(double number, register int prec, int flags, int *signp,
  655.          int fmtch, char *startp, char *endp)
  656. {
  657.     register char *p, *t;
  658.     register double fract;
  659.     int dotrim = 0, expcnt, gformat = 0;
  660.     double integer, tmp;
  661.  
  662.     expcnt = 0;
  663.     if (number < 0) {
  664.         number = -number;
  665.         *signp = '-';
  666.     } else
  667.         *signp = 0;
  668.  
  669.     fract = modf(number, &integer);
  670.  
  671.     /* get an extra slot for rounding. */
  672.     t = ++startp;
  673.  
  674.     /*
  675.      * get integer portion of number; put into the end of the buffer; the
  676.      * .01 is added for modf(356.0 / 10, &integer) returning .59999999...
  677.      */
  678.     for (p = endp - 1; integer; ++expcnt) {
  679.         tmp = modf(integer / 10, &integer);
  680.         *p-- = to_char((int)((tmp + .01) * 10));
  681.     }
  682.     switch (fmtch) {
  683.     case 'f':
  684.     case 'F':
  685.         /* reverse integer into beginning of buffer */
  686.         if (expcnt)
  687.             for (; ++p < endp; *t++ = *p);
  688.         else
  689.             *t++ = '0';
  690.         /*
  691.          * if precision required or alternate flag set, add in a
  692.          * decimal point.
  693.          */
  694.         if (prec || flags&ALT)
  695.             *t++ = '.';
  696.         /* if requires more precision and some fraction left */
  697.         if (fract) {
  698.             if (prec)
  699.                 do {
  700.                     fract = modf(fract * 10, &tmp);
  701.                     *t++ = to_char((int)tmp);
  702.                 } while (--prec && fract);
  703.             if (fract)
  704.                 startp = round(fract, (int *)NULL, startp,
  705.                     t - 1, (char)0, signp);
  706.         }
  707.         for (; prec--; *t++ = '0');
  708.         break;
  709.     case 'e':
  710.     case 'E':
  711. eformat:    if (expcnt) {
  712.             *t++ = *++p;
  713.             if (prec || flags&ALT)
  714.                 *t++ = '.';
  715.             /* if requires more precision and some integer left */
  716.             for (; prec && ++p < endp; --prec)
  717.                 *t++ = *p;
  718.             /*
  719.              * if done precision and more of the integer component,
  720.              * round using it; adjust fract so we don't re-round
  721.              * later.
  722.              */
  723.             if (!prec && ++p < endp) {
  724.                 fract = 0;
  725.                 startp = round((double)0, &expcnt, startp,
  726.                     t - 1, *p, signp);
  727.             }
  728.             /* adjust expcnt for digit in front of decimal */
  729.             --expcnt;
  730.         }
  731.         /* until first fractional digit, decrement exponent */
  732.         else if (fract) {
  733.             /* adjust expcnt for digit in front of decimal */
  734.             for (expcnt = -1;; --expcnt) {
  735.                 fract = modf(fract * 10, &tmp);
  736.                 if (tmp)
  737.                     break;
  738.             }
  739.             *t++ = to_char((int)tmp);
  740.             if (prec || flags&ALT)
  741.                 *t++ = '.';
  742.         }
  743.         else {
  744.             *t++ = '0';
  745.             if (prec || flags&ALT)
  746.                 *t++ = '.';
  747.         }
  748.         /* if requires more precision and some fraction left */
  749.         if (fract) {
  750.             if (prec)
  751.                 do {
  752.                     fract = modf(fract * 10, &tmp);
  753.                     *t++ = to_char((int)tmp);
  754.                 } while (--prec && fract);
  755.             if (fract)
  756.                 startp = round(fract, &expcnt, startp,
  757.                     t - 1, (char)0, signp);
  758.         }
  759.         /* if requires more precision */
  760.         for (; prec--; *t++ = '0');
  761.  
  762.         /* unless alternate flag, trim any g/G format trailing 0's */
  763.         if (gformat && !(flags&ALT)) {
  764.             while (t > startp && *--t == '0');
  765.             if (*t == '.')
  766.                 --t;
  767.             ++t;
  768.         }
  769.         t = exponent(t, expcnt, fmtch);
  770.         break;
  771.     case 'g':
  772.     case 'G':
  773.         /* a precision of 0 is treated as a precision of 1. */
  774.         if (!prec)
  775.             ++prec;
  776.         /*
  777.          * ``The style used depends on the value converted; style e
  778.          * will be used only if the exponent resulting from the
  779.          * conversion is less than -4 or greater than the precision.''
  780.          *    -- ANSI X3J11
  781.          */
  782.         if (expcnt > prec || (!expcnt && fract && fract < .0001)) {
  783.             /*
  784.              * g/G format counts "significant digits, not digits of
  785.              * precision; for the e/E format, this just causes an
  786.              * off-by-one problem, i.e. g/G considers the digit
  787.              * before the decimal point significant and e/E doesn't
  788.              * count it as precision.
  789.              */
  790.             --prec;
  791.             fmtch -= 2;        /* G->E, g->e */
  792.             gformat = 1;
  793.             goto eformat;
  794.         }
  795.         /*
  796.          * reverse integer into beginning of buffer,
  797.          * note, decrement precision
  798.          */
  799.         if (expcnt)
  800.             for (; ++p < endp; *t++ = *p, --prec);
  801.         else
  802.             *t++ = '0';
  803.         /*
  804.          * if precision required or alternate flag set, add in a
  805.          * decimal point.  If no digits yet, add in leading 0.
  806.          */
  807.         if (prec || flags&ALT) {
  808.             dotrim = 1;
  809.             *t++ = '.';
  810.         }
  811.         else
  812.             dotrim = 0;
  813.         /* if requires more precision and some fraction left */
  814.         if (fract) {
  815.             if (prec) {
  816.                 /* If no integer part, don't count initial
  817.                  * zeros as significant digits. */
  818.                 do {
  819.                     fract = modf(fract * 10, &tmp);
  820.                     *t++ = to_char((int)tmp);
  821.                 } while(!tmp && !expcnt);
  822.                 while (--prec && fract) {
  823.                     fract = modf(fract * 10, &tmp);
  824.                     *t++ = to_char((int)tmp);
  825.                 }
  826.             }
  827.             if (fract)
  828.                 startp = round(fract, (int *)NULL, startp,
  829.                     t - 1, (char)0, signp);
  830.         }
  831.         /* alternate format, adds 0's for precision, else trim 0's */
  832.         if (flags&ALT)
  833.             for (; prec--; *t++ = '0');
  834.         else if (dotrim) {
  835.             while (t > startp && *--t == '0');
  836.             if (*t != '.')
  837.                 ++t;
  838.         }
  839.     }
  840.     return (t - startp);
  841. }
  842.  
  843. #endif /* defined(FLOATING_POINT) && !defined(USE_DTOA) */
  844.